package com.tarena.practice.manage.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tarena.practice.manage.pojo.Topic;
import com.tarena.practice.manage.service.TopicService;
import com.tarena.practice.manage.vo.PageUtils;
import com.tarena.practice.manage.vo.SysResult;

@Controller
@RequestMapping("topic/")
public class TopicController {

	@Autowired
	private TopicService topicService;

	/**
	 * 分页查询页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("query")
	public String topicQuery(Model model) {
		return "topic";
	}

	/**
	 * 题目分页查询
	 * 
	 * @param page
	 * @param pageSize
	 * @param type
	 * @param keyword
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "query/page", method = RequestMethod.POST)
	@ResponseBody
	public PageUtils<Topic> toTopicQuery(@RequestParam(name = "pageNum", defaultValue = "1") Integer page,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
			@RequestParam(name = "type", defaultValue = "0", required = false) Integer type,
			@RequestParam(name = "keyWord", defaultValue = "", required = false) String keyword,
			@RequestParam(name = "order", defaultValue = "ASC", required = false) String order) {
		PageUtils<Topic> result = topicService.quertTopicPage(page, pageSize, keyword, order, type);
		return result;
	}

	/**
	 * 题目删除
	 * 
	 * @param topicId
	 * @return
	 */
	@RequestMapping("delete/{topicId}")
	@ResponseBody
	public SysResult delTopic(@PathVariable("topicId") Integer topicId) {
		SysResult sysResult = topicService.deleteTopicById(topicId);
		return sysResult;
	}

	/**
	 * 题目编辑页
	 * @param topicId
	 * @param model
	 * @return
	 */
	@RequestMapping("editPage")
	public String editTopicPahe(@RequestParam(value="topicId", required=false)Integer topicId, Model model) {
		Topic topic = topicService.queryTopicById(topicId);
		model.addAttribute("topic", topic);
		return "editTopic1";
	}

	
	/**
	 * 題目新增/編輯
	 */
	@RequestMapping(value="add",method=RequestMethod.POST)
	@ResponseBody
	public SysResult addTopic(Topic topic) {
		
		SysResult sysResult = SysResult.oK();
		// 判断对题目进行哪种操作
		if(topic.getTopicId() != null) {
			sysResult = topicService.editTopic(topic);
		} else {
			sysResult = topicService.addTopic(topic);
		}
		
		return sysResult;
	}
	
	@RequestMapping("topicDetail")
	public String topicDetail(@RequestParam(value="topicId", required=true)Integer topicId, Model model) {
		Topic topic = topicService.queryTopicById(topicId);
		model.addAttribute("topic", topic);
		return "topicDetail";
	}

	
	// @RequestMapping("gent")
	public void generate() {
		topicService.generate();
	}
}
